package com.lu.controller;

import com.github.pagehelper.Page;
import com.lu.model.enums.ApprovalStatusEnum;
import com.lu.model.domain.ProcessApprovalLog;
import com.lu.model.domain.ProcessApprovalWaitList;
import com.lu.model.entity.SysApprovalConfig;
import com.lu.model.entity.SysApprovalLog;
import com.lu.model.request.ProcessApprovalQueryRequest;
import com.lu.model.request.ProcessApprovalSubmitRequest;
import com.lu.model.request.ProcessApprovalUpdateInfoRequest;
import com.lu.model.response.Result;
import com.lu.service.SysApprovalService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统审批日志 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-08-01
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-approval")
public class SysApprovalController {

  private static final String PROCESS = "/process";
  private static final String CONFIG = "/config";
  private static final String LOG = "/log";

  private final SysApprovalService approvalService;

  /**
   * 刷新审批配置
   * @return
   */
  @GetMapping(CONFIG + "/refresh")
  public Result<Map<String, SysApprovalConfig>> refreshConfig(){
    return Result.success(approvalService.refreshConfig());
  }

  /**
   * 查询所有的配置，根据table_name分组聚合 - 以便告诉上层，我们系统有这么多审批流程
   * @return
   */
  @GetMapping(CONFIG + "/list")
  public Result<List<SysApprovalConfig>> configList(){
    return Result.success(approvalService.configList());
  }

  /**
   * 提交审批
   *  - 按照配置，处理每个节点的审批
   *  - 最终将表状态改成审批后的状态
   *  - 整个过程记录日志
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-approval/process/submit')")
  @PostMapping(PROCESS + "/submit")
  public Result<String> submit(@RequestBody @Valid ProcessApprovalSubmitRequest request){
    return Result.success(approvalService.submit(request));
  }

  /**
   * 修改审批主表信息
   *  - 修改审核信息，将status设置为 审核中，即重新发起审核流程
   *  - 具体设置成什么值，根据配置上来的
   *  - 本系统统一参考： @see {@link ApprovalStatusEnum#UN_AUDITED} {@link ApprovalStatusEnum#NO_PASS}
   * @param request
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-approval/update-info')")
  @PostMapping("/update-info")
  public Result updateInfo(@RequestBody @Valid ProcessApprovalUpdateInfoRequest request){
    return Result.success(approvalService.updateInfo(request));
  }

  /**
   * 查询审批日志 【正在审批的，审批结束的】
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-approval/log/page-list')")
  @PostMapping(LOG + "/page-list")
  public Result<Page<ProcessApprovalLog>> pageList(@RequestBody @Valid ProcessApprovalQueryRequest request){
    return Result.success(approvalService.pageList(request));
  }

  /**
   * 待办
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-approval/log/wait-list')")
  @PostMapping(LOG + "/wait-list")
  public Result<List<ProcessApprovalWaitList>> waitList(@RequestBody @Valid ProcessApprovalQueryRequest request){
    return Result.success(approvalService.waitList(request));
  }

  /**
   * 查询实体审批全不流程
   *  - 查询 业务表 数据行 每个节点的审批详情
   * @return
   */
  @PostMapping(LOG + "/detail")
  public Result<List<SysApprovalLog>> detail(@RequestBody @Valid ProcessApprovalQueryRequest request){
    return Result.success(approvalService.detail(request));
  }

  /**
   * 查询实体信息
   * @param request
   * @return
   */
  @PostMapping(PROCESS + "/get")
  public Result get(@RequestBody @Valid ProcessApprovalQueryRequest request){
    return Result.success(approvalService.get(request));
  }

}
